/******************************************************************************************
 * Import
 ******************************************************************************************/

// module
import Taro, { Component } from '@tarojs/taro';
import { Swiper } from '@tarojs/components';
import { AtTextarea } from 'taro-ui';

// project
import { CPage, CContent, CHeader, CFooter, CFloat, CAround, CBetween, CCenter, CRow, CWrap, CLine, CHand, CItemX, CItemY, CText, CLabel, CSvg, CImage, CButton, CColumn } from '../../library/comp';
import { Base64, Gen, Time, Zip, Session, Storage, Toast} from '../../library/func';
import Icon from '../../project/style/icon';
import Http from '../../project/func/http/http';
import '../../library/style/patch.css';


/******************************************************************************************
 * Export Class
 ******************************************************************************************/

export default class EditIndex extends Component {
    constructor(props) {
        super(props);

        /**
         * state
         */
        this.state = {
            // 文本框内容
            value:this.$router.params.value?this.$router.params.value:'',
            // 判断是输入文字还是内容
            type: this.$router.params.type,
            key: this.$router.params.key ? this.$router.params.key : null,
            settype:this.$router.params.settype ? this.$router.params.key : "add",
        }
    }

	//onNavigateBack
	onNavigateBack(num=1){
		wx.navigateBack({
			delta: num
		})                    
	}


    /*------------------------------------------------------------------------------------
    * 声明周期
    *-----------------------------------------------------------------------------------*/
    componentDidMount(){
    }

    /*------------------------------------------------------------------------------------
    * 自定义方法
    *-----------------------------------------------------------------------------------*/
    // input绑定value
    changeValue = e => {
        this.setState({
            value:e.target.value
        })
    }
    // 点击完成时触发方法
    onConfirm = e => {

        if(this.state.value == ""){
            Toast.error("不能为空");
            return;
        }

        if(this.state.type == "content"){
            let addobj = {
                type: "text",
                content: this.state.value
            };
            
            Session.set("set_content",{
                item:addobj,
                key:this.state.key,
                settype:this.state.settype
            })

        }

        if(this.state.type == "title"){

            Session.set("set_title",{
                set_title:this.state.value,
            })

        }


        Taro.navigateBack({ delta: 1 })

    }



    /**
     * 渲染
     */
    render() {
        return (
            <CPage style='height:100vh'>
                <CContent style='height:100vh' bg='#fff'>

                    {this.state.type == 'content' ? 
                        
                    <AtTextarea
                        value={this.state.value}
                        onChange={this.changeValue.bind(this)}
                        onConfirm={this.onConfirm.bind(this)}
                        maxlength='5000'
                        autoFocus={true}
                        showConfirmBar
                        placeholder= '请输入文章内容'
                        class='borderNone'
                        height='400'
                    />

                    :

                    <AtTextarea
                        value={this.state.value}
                        onChange={this.changeValue.bind(this)}
                        onConfirm={this.onConfirm.bind(this)}
                        maxlength='50'
                        autoFocus={true}
                        showConfirmBar
                        placeholder='请输入文章标题'
                        class='borderNone'
                        height='400'
                    />

                    }


                    <CButton onClick={ this.onConfirm } bg="green" py="10" ra="10" color="white">保存</CButton>
                    
                </CContent>
            </CPage>
        );
    }
}
